<template lang="html">
  <el-dialog :close-on-click-modal="closeonclickmodal" width="480px" :title="getdialogTitle()" @open="listenopen()"
             @close="close" :visible="dialogshow">
    <el-form :id="target"  :model="dialoginfo" ref="dialoginfo" label-position="left">
      <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                    prop="userName" label="账号" :label-width="formLabelWidth">
        <el-input :disabled="!isadd"  v-model="dialoginfo.userName" auto-complete="off" clearable/>
      </el-form-item>
      <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                    prop="password" :label="isadd?'密码':'新密码'" :label-width="formLabelWidth">
        <el-input type="password" v-model="dialoginfo.password" auto-complete="off" clearable></el-input>
      </el-form-item>
      <el-row>
        <el-form-item style="text-align: right">
          <el-button size="mini" @click="dialogshow = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="toAdd('dialoginfo')">确 定</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
  export default {
    inject: ["reload"],
    data() {
      return {
        target:"userEdit",
        closeonclickmodal: true,
        formLabelWidth: '80px'
      }
    },
    props: ['dialogshow', 'dialoginfo', 'isadd'],
    methods: {
      getdialogTitle() {
        return this.isadd == true ? "新增系统账号" : "修改密码";
      },
      listenopen() {
        if (this.isadd && this.$refs['dialoginfo'] != null) {
          this.$refs['dialoginfo'].resetFields();
        }
        // console.log(this.isadd);
        // console.log(this.dialogshow);
      },
      close() {
        this.$emit('changedialogshow');
      },
      toAdd(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var t_data = {};
            if(this.isadd){
              t_data.password = this.proUtil.md5(this.proUtil.md5(this.dialoginfo.password));
              t_data.userName = this.dialoginfo.userName;
            }else{
              t_data.newPassword = this.proUtil.md5(this.proUtil.md5(this.dialoginfo.password));
              t_data.userId = this.dialoginfo.id;
            }
            this.$store.commit("saveLoadingMap",{"target":this.isadd ?this.proAPI.URLS.addSysUserAccount : this.proAPI.URLS.modifyAccountPwdByUserId,"id":this.target});
            this.proAPI.postJson(this.isadd ? this.proAPI.URLS.addSysUserAccount : this.proAPI.URLS.modifyAccountPwdByUserId, t_data).then((res) => {
                // console.log(res);
                this.$message.success({
                  "message": res.message
                })
                setTimeout(() => {
                  this.reload();

                }, 1000)
            });
          } else {
            return false;
          }
        });
      }
    },
    updated() {
      // console.log(333);
    },
    mounted() {
      // console.log("开始创建！！！！！！！！！！！！！！！！！");
    }
  };
</script>
<style lang="css">
</style>
